﻿using System;
using System.Collections.Generic;

using Newtonsoft.Json.Linq;

using EHSubmit.Model;
using EHSubmit.Util;

namespace EHSubmit.Logic.Services
{

    /// <summary>
    /// 转换json本文为所需数据类型实例
    /// </summary>
    public class DataConverter
    {

        /// <summary>
        /// 把JSON文本转换为User对象
        /// </summary>
        /// <param name="json">json文本</param>
        /// <returns>User对象</returns>
        public static User ParseUser(string json)
        {
            if (json == "{}")
            {
                return null;
            }

            try
            {
                JObject jo = JObject.Parse(json);

                User user = new User();

                user.ID = (string)jo["ID"];
                user.Name = (string)jo["Name"];
                user.Sex = (string)jo["Sex"];
                user.Institute = (string)jo["Institute"];
                user.Department = (string)jo["Department"];
                user.Class = (string)jo["Class"];

                return user;
            }
            catch (Exception e)
            {
                LogHelper.Log("从教务管理系统：json转换错误", e);
                throw new SysException("从教务管理系统传来的数据格式有误");
            }
        }

        /// <summary>
        /// 把JSON文本转换为User集合
        /// </summary>
        /// <param name="json">json文本</param>
        /// <returns>User集合</returns>
        public static IList<User> ParseUsers(string json)
        {
            if (json == "{}")
            {
                return null;
            }

            IList<Object> remoteUsers = null;
            IList<User> localUsers = new List<User>();
            try
            {
                remoteUsers = JsonHelper.Deserialize<IList<Object>>(json);
                foreach (JObject remoteUser in remoteUsers)
                {
                    User localUser = new User();

                    localUser.ID = (string)remoteUser["ID"];
                    localUser.Name = (string)remoteUser["Name"];
                    localUser.Sex = (string)remoteUser["Sex"];
                    localUser.Institute = (string)remoteUser["Institute"];
                    localUser.Department = (string)remoteUser["Department"];
                    localUser.Class = (string)remoteUser["Class"];

                    localUsers.Add(localUser);
                }

                return localUsers;
            }
            catch (Exception e)
            {
                LogHelper.Log("从教务管理系统：json转换错误", e);
                throw new SysException("从教务管理系统传来的数据格式有误");
            }
        }
        /// <summary>
        /// 把JSON文本转换为Course集合
        /// </summary>
        /// <param name="json">JSON数据</param>
        /// <returns>课程信息集合</returns>
        public static IList<Course> ParseCourses(string json)
        {
            if (json == "{}")
            {
                return null;
            }

            IList<Course> courses = null;
            try
            {
                courses = JsonHelper.Deserialize<IList<Course>>(json);
                return courses;
            }
            catch (Exception e)
            {
                LogHelper.Log("从教务管理系统：json转换错误", e);
                throw new SysException("从教务管理系统传来的数据格式有误");
            }
        }

        /// <summary>
        /// 把JSON文本转换为BaseCourse集合
        /// </summary>
        /// <param name="json">JSON数据</param>
        /// <returns>课程信息集合(不是课程班级信息)</returns>
        public static IList<BaseCourse> ParseBaseCourses(string json)
        {
            if (json == "{}")
            {
                return null;
            }

            IList<BaseCourse> baseCourses = null;
            try
            {
                baseCourses = JsonHelper.Deserialize<IList<BaseCourse>>(json);
                return baseCourses;
            }
            catch (Exception e)
            {
                LogHelper.Log("从教务管理系统：json转换错误", e);
                throw new SysException("从教务管理系统传来的数据格式有误");
            }
        }



    }
}